package com.lecture.javamode.oop.virtualWallet.ddd;

import java.math.BigDecimal;

/**
 * @author luyn
 * @version 1.0
 * @description Domain领域模型(充血模型)
 * @date 2022/1/10 13:27
 */

public class VirtualWallet {
    private Long id;
    private Long createTime = System.currentTimeMillis();
    private BigDecimal balance = BigDecimal.ZERO;
    private boolean isAllowedOverdraft = true;
    private BigDecimal overdraftAmount = BigDecimal.ZERO;
    private BigDecimal frozenAmount = BigDecimal.ZERO;

    public VirtualWallet(Long preAllocatedId) {
        this.id = preAllocatedId;
    }

    //其他的业务逻辑。。。。
    public void freeze(BigDecimal amount) {  }
    public void unfreeze(BigDecimal amount) { }
    public void increaseOverdraftAmount(BigDecimal amount) {  }
    public void decreaseOverdraftAmount(BigDecimal amount) {  }
    public void closeOverdraft() {  }
    public void openOverdraft() {  }

    public BigDecimal balance() {
        return this.balance;
    }

    public void debit(BigDecimal amount) {
        if (this.balance.compareTo(amount) < 0) {
            throw new RuntimeException("余额不足");
        }
        this.balance = this.balance.subtract(amount);
    }

    public void credit(BigDecimal amount) {
        if (amount.compareTo(BigDecimal.ZERO) < 0) {
            throw new RuntimeException("余额不足");
        }
        this.balance = this.balance.add(amount);
    }

    public BigDecimal getAvaliableBalance() {
        BigDecimal totalAvaliableBalance = this.balance.subtract(this.frozenAmount);
        if (isAllowedOverdraft) {
            //totalAvaliableBalance += this.overdraftAmount;
        }
        return totalAvaliableBalance;
    }
}

